//
//  TbDetailVC.swift
//  LeGou
//
//  Created by lxm on 2022/7/28.
//

import UIKit
import WebKit

class LessonDetail: ZMViewController {

    //MARK:--------------------属性--------------------
    var id = ""
    
    var info = ZMUtil.fastBehavior(value: VIPMallModels.GoodsDetailResponse())
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var banner: SDCycleScrollView!
    
    @IBOutlet weak var price: UILabel!
    
    @IBOutlet weak var name: UILabel!
    
    @IBOutlet weak var sales: UILabel!
    
    @IBOutlet weak var detailContent: UIView!
    
    @IBOutlet weak var detialH: NSLayoutConstraint!
    
    let loading = XMLoadingView()
    
    let web = WKWebView()
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
        loading.showInView(fatherView: view)
        getDetial()
    }
    //MARK:--------------------UI--------------------
    func setUI() {
//        hbd_barHidden = true
//        navView.contentView.backgroundColor = .clear
//        navView.backgroundColor = .clear
//        navView.autoBack = true
//        navView.backButton.setImage(UIImage.init(named: "detail_back"), for: .normal)
//        navView.backButton.backgroundColor = UIColor.zm.RGBA(r: 0, g: 0, b: 0, a: 0.55)
//        navView.backButton.snp.updateConstraints { make in
//            make.width.height.equalTo(36)
//        }
//        navView.backButton.layer.cornerRadius = 18
        title = "商品详情"
        banner.imageURLStringsGroup = ["http://yinongzhenxuan.oss-cn-beijing.aliyuncs.com/img/8799405d8f024d289bfce6b8f6695d56.png", "http://yinongzhenxuan.oss-cn-beijing.aliyuncs.com/img/8799405d8f024d289bfce6b8f6695d56.png"]
        banner.pageDotColor = "#999999".zm.hex()
        banner.currentPageDotColor = Style.mainColor
        banner.bannerImageViewContentMode = .scaleAspectFill
        
        
        // web
        detailContent.addSubview(web)
        web.scrollView.isScrollEnabled = false 
        web.scrollView.rx.observe(CGSize.self, "contentSize").subscribe(onNext:{ [unowned self] resp in
            if let size = resp {
                let h = size.height
                if self.web.superview != nil {
                   self.detialH.constant = h
                }
            }
        }).disposed(by: disposeBag)
        web.frame = CGRect(x: 12, y: 12, width: SCREEN_W - 24, height: 10)
        web.layer.cornerRadius = 8
        web.layer.masksToBounds = true
//        web.load(URLRequest.init(url: URL.init(string: treeExchangeIntroH5)!))
    }
    func addConstraints() {
        web.snp.makeConstraints { make in
            make.edges.equalToSuperview()
            
        }
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        self.info.subscribe(onNext:{ [unowned self] resp in
            let goods = resp.productInfo
            self.price.attributedText = "¥".zm.attributedStr(attrs: [.font : UIFont.systemFont(ofSize: 14, weight: .medium)])
                .zm.append(str: goods.price.zm.toString(keep: 2), font: UIFont.systemFont(ofSize: 22, weight: .semibold))
            self.name.text = goods.storeName
            self.sales.text = goods.storeInfo
            if let imgs = goods.sliderImage.zm.toArray() {
                self.banner.imageURLStringsGroup = imgs
            }
            self.web.loadHTMLString(goods.content.zm.htmlBigSizeString(), baseURL: nil)
        }).disposed(by: disposeBag)
    }

    func getDetial() {
        VIPMallAPI.goodsDetail(id).rxRequest(type: VIPMallModels.GoodsDetailResponse.self).subscribe(onNext:{ [unowned self] resp in
            self.loading.hideView(anmation: true)
            self.info.accept(resp)
        }, onError: { e in
            self.loading.showError(e.localizedDescription)
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onBuy(_ sender: Any) {
        // 预xiadan
        if !LoginService.shared.checkLogin() {
            return
        }
        guard let atrrs = info.value.productAttr, let firstAtrr = atrrs.first, let aId = JSON(info.value.productValue)[firstAtrr.attrValues]["id"].int else {
            ZMToast.showToast(title: "没有获取到商品属性信息")
            return
        }
        ZMToast.showActivity()
        VIPMallAPI.preOrder(["attrValueId" : aId, "productId" : id]).rxRequest(type: [String : String].self).subscribe(onNext:{ [unowned self] resp in
            ZMToast.hiddenActivity()
            let vc = CreateOrder()
            vc.orderNo = JSON(resp)["preOrderNo"].stringValue
            self.navigationController?.pushViewController(vc, animated: true)
        }).disposed(by: disposeBag)
    }
    
    
    
    //MARK:--------------------delegate--------------------

}
